package com.example.hdx.controller;

import com.example.hdx.DOT.CartAddDTO;
import com.example.hdx.DOT.CartUpdateDTO;
import com.example.hdx.entity.Cart;
import com.example.hdx.service.CartService;
import com.example.hdx.util.FwResult;
import com.example.hdx.util.JwtConstant;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import com.example.hdx.util.JwtUtil;
import java.util.List;

//@RestController
//@RequestMapping("/cart")
//@Slf4j
//public class CartController {
//
//    @Autowired
//    private CartService cartService;
//
//    @GetMapping
//    public FwResult<List<Cart>> getCart(HttpSession session) {
//        // 从session获取用户ID
//        Long userId = (Long) session.getAttribute("userId");
//        if (userId == null) {
//            return FwResult.tokenFailedMsg("用户未登录");
//        }
//        return cartService.getUserCart(userId);
//    }
//
//    @PostMapping("/add")
//    public FwResult<Boolean> addToCart(@RequestBody CartAddDTO dto, HttpSession session) {
//        Long userId = (Long) session.getAttribute("userId");
//        if (userId == null) {
//            return FwResult.tokenFailedMsg("用户未登录");
//        }
//        return cartService.addToCart(userId, dto.getProductId(), dto.getQuantity());
//    }
//
//    @PutMapping("/{productId}")
//    public FwResult<Boolean> updateQuantity(
//            @PathVariable Long productId,
//            @RequestBody CartUpdateDTO dto,
//            HttpSession session) {
//        Long userId = (Long) session.getAttribute("userId");
//        if (userId == null) {
//            return FwResult.tokenFailedMsg("用户未登录");
//        }
//        return cartService.updateQuantity(userId, productId, dto.getQuantity());
//    }
//
//    @DeleteMapping("/{productId}")
//    public FwResult<Boolean> removeFromCart(@PathVariable Long productId, HttpSession session) {
//        Long userId = (Long) session.getAttribute("userId");
//        if (userId == null) {
//            return FwResult.tokenFailedMsg("用户未登录");
//        }
//        return cartService.removeFromCart(userId, productId);
//    }
//}

@RestController
@RequestMapping("/cart")
@Slf4j
public class CartController {

    @Autowired
    private CartService cartService;

    /**
     * 获取用户的购物车列表
     */
//    @GetMapping("/get")
//    public FwResult<List<Cart>> getCart(HttpSession session) {
//
//        Long userId = (Long) session.getAttribute("userId");
//        if (userId == null) {
//            return FwResult.tokenFailedMsg("用户未登录");
//        }
//        System.out.println("当前登录用户ID：" + userId);
//        return cartService.getUserCart(userId);
//    }
    @GetMapping("/get")
    public FwResult<List<Cart>> getCart(HttpServletRequest request) {
        String token = request.getHeader(JwtConstant.HEADER_NAME); // "Authorization"
        if (!StringUtils.hasText(token)) {
            return FwResult.tokenFailedMsg("缺少 Token");
        }

        try {
            Long userId = JwtUtil.getUserIdFromToken(token);
            System.out.println("当前登录用户ID：" + userId);
            return cartService.getUserCart(userId);
        } catch (Exception e) {
            return FwResult.tokenFailedMsg("无效 Token");
        }

    }

    /**
     * 添加商品到购物车
     */
    @PostMapping("/add")
    public FwResult<Boolean> addToCart(@RequestBody CartAddDTO dto,HttpServletRequest request) {
        String token = request.getHeader(JwtConstant.HEADER_NAME); // "Authorization"

        if (!StringUtils.hasText(token)) {
            return FwResult.tokenFailedMsg("缺少 Token");
        }
        Long userId = JwtUtil.getUserIdFromToken(token);
        if (token == null) {
            return FwResult.tokenFailedMsg("用户未登录");
        }
        return cartService.addToCart(userId, dto.getProductId(), dto.getQuantity());

    }

    /**
     * 删除购物车中的某个商品
     */
    @DeleteMapping("/{productId}")
    public FwResult<Boolean> removeFromCart(@PathVariable Long productId, HttpServletRequest request) {
        String token = request.getHeader(JwtConstant.HEADER_NAME); // "Authorization"
        if (!StringUtils.hasText(token)) {
            return FwResult.tokenFailedMsg("缺少 Token");
        }
        Long userId = JwtUtil.getUserIdFromToken(token);
        if (userId == null) {
            return FwResult.tokenFailedMsg("用户未登录");
        }
        return cartService.removeFromCart(userId, productId);
    }




    /**
     * 更新购物车中商品的数量
     */
    @PutMapping("/{productId}")
    public FwResult<Boolean> updateQuantity(
            @PathVariable Long productId,
            @RequestBody CartUpdateDTO dto,
            HttpServletRequest request) {
        String token = request.getHeader(JwtConstant.HEADER_NAME); // "Authorization"
        if (!StringUtils.hasText(token)) {
            return FwResult.tokenFailedMsg("缺少 Token");
        }
        Long userId = JwtUtil.getUserIdFromToken(token);
        if (userId == null) {
            return FwResult.tokenFailedMsg("用户未登录");
        }
        return cartService.updateQuantity(userId, productId, dto.getQuantity());
    }

    /**
     * 清空当前用户的购物车
     */

}